package com.wao.cms.service;

import com.wao.cms.domain.Page;
import com.wao.cms.repository.PageRepository;
import com.wao.cms.service.dto.PageDTO;
import com.wao.cms.service.mapper.PageMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service Implementation for managing Page.
 */
@Service
@Transactional
public class PageService {

    private final Logger log = LoggerFactory.getLogger(PageService.class);

    private final String IMG_SOURCE_STR = "<X_X_X";

    private final String IMG_TARGET_STR = "<img";

    private final PageRepository pageRepository;

    private final PageMapper pageMapper;

    public PageService(PageRepository pageRepository, PageMapper pageMapper) {
        this.pageRepository = pageRepository;
        this.pageMapper = pageMapper;
    }

    /**
     * Save a page.
     *
     * @param pageDTO the entity to save
     * @return the persisted entity
     */
    public PageDTO save(PageDTO pageDTO) {
        log.debug("Request to save Page : {}", pageDTO);
        Page page = pageMapper.toEntity(pageDTO);
        if (StringUtils.isNotBlank(page.getContent()) && page.getContent().contains(IMG_SOURCE_STR))
            page.setContent(page.getContent().replace(IMG_SOURCE_STR, IMG_TARGET_STR));
        page = pageRepository.save(page);
        return pageMapper.toDto(page);
    }

    /**
     * Get all the pages.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public org.springframework.data.domain.Page<PageDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Pages");
        return pageRepository.findAll(pageable)
                .map(pageMapper::toDto);
    }

    /**
     * Get one page by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public PageDTO findOne(Long id) {
        log.debug("Request to get Page : {}", id);
        Page page = pageRepository.findOne(id);
        return pageMapper.toDto(page);
    }

    /**
     * Delete the  page by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Page : {}", id);
        pageRepository.delete(id);
    }

    /**
     * Get Pages by Website.
     *
     * @param pageable  the pagination information
     * @param websiteId the id of the Website
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public org.springframework.data.domain.Page<PageDTO> findByWebsiteAndKeyword(Long websiteId,
                                                                                 String keyword,
                                                                                 String url,
                                                                                 Pageable pageable) {
        log.debug("Request to get Pages by Website: {}", websiteId);
        return pageRepository.findByWebsiteIdAndNameLikeAndUrlLike(websiteId, "%" + keyword + "%", "%" + url + "%", pageable)
                .map(pageMapper::toDto);
    }
}
